﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ViewCart : System.Web.UI.Page
{   
    ShoppingCart cart;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.cart = (ShoppingCart)Session["ShoppingCart"];
        if (!IsPostBack)
        {
            this.FillShoppingCart();
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string maxe = GridView1.Rows[e.RowIndex].Cells[0].Text;
        this.cart.Remove(maxe);
        this.FillShoppingCart();
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow ViewRow in GridView1.Rows)
        {
            string maxe = ViewRow.Cells[0].Text;
            TextBox txtSoLuongMua = (TextBox)ViewRow.Cells[3].FindControl("txtSoLuongMua");
            if (txtSoLuongMua.Text != "")
            {
                int soluong = int.Parse(txtSoLuongMua.Text);
                this.cart.Update(maxe, soluong);
            }         
        }
        this.FillShoppingCart();
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        this.cart.Clear();
        this.FillShoppingCart();        
    }

    protected void btnCheckout_Click(object sender, EventArgs e)
    {
        Response.Redirect("Checkout.aspx");
    }

    void FillShoppingCart()
    {
        GridView1.DataSource = this.cart;
        GridView1.DataBind();

        MasterPage master = (MasterPage)this.Master;
        master.RefreshShoppingCartInfo();

        if (this.cart.Count > 0)
        {
            GridView1.FooterRow.Cells[4].Text = "$ " + this.cart.Total.ToString("#.#0");
        }

        bool hasRow = cart.Count > 0;
        btnUpdate.Enabled = hasRow;
        btnClear.Enabled = hasRow;
        btnContinue.Enabled = hasRow;
        btnCheckout.Enabled = (hasRow && Session["Userlogin"] != null);
    }

    protected void btnContinue_Click(object sender, EventArgs e)
    {
        Response.Redirect("Product.aspx");
    }
}
